﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="SampleSite.template._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="/css/Site.css" rel="stylesheet" type="text/css" />
    <link href="/css/themes/metro/easyui.css" rel="stylesheet" type="text/css" />
    <link href="/css/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="/scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="/scripts/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="/scripts/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="/scripts/DialogHelper.js"></script>

    <style type="text/css">
        .tb_grid { text-align:left;border:0px solid #0094ff;border-collapse: collapse; width:100%; }
        .tb_grid td{ border-bottom:1px solid #95B8E7; }
        .tb_grid tr{ height:30px; }
        .td_lbl{ width:10%; background:#6caef5; text-align:center;}
        .td_cnt{ width:35%; text-align:left; background:#E0ECFF;}
    </style>
</head>
<body class="easyui-layout">
    <div>
	<table id="table" class="easyui-datagrid" title="<%=this.Table.DisplayName %>" 
		   data-options="idField:'Id', singleSelect:true, rownumbers: true, pagination: true,collapsible:true,url:'/dyajax/<%=this.Table.Name %>/query',toolbar:toolbar">
		<thead>
			<tr>
				<th data-options="field:'Id',width:80, hidden:true">ID</th>

                <% foreach (var col in this.Table.Columns.Where(k => !k.IsSystemDefault)) { %>
                    <th data-options="field:'<%=col.Name %>', width:100<%=this.GetAttributes(col) %>"><%=col.DisplayName %></th>                   
                <% } %>
<%--                <th data-options="field:'CreateName',width:80">创建人</th>
                <th data-options="field:'UpdatedName',width:80">最后修改人</th>
                <th data-options="field:'CreationTime',width:140,formatter:formatter_datetime">创建时间</th>
                <th data-options="field:'UpdatedTime',width:140,formatter:formatter_datetime">最后修改时间</th>--%>
			</tr>
		</thead>
	</table>
    </div>

    <div id="dlgAdd"
         class="easyui-dialog"
         style="width:800px;height:600px;padding:0px;"
         data-options="closed:true,resizable:true,modal:true,buttons:'#dlg-buttons',align:'center'">
        
        <form action="/dyajax/<%=this.Table.Name %>/add" method="post">
            <table id="tblAdd" class="tb_grid" >
                <tr style="height:20px;"><td colspan="4">&nbsp;</td></tr>
                <%  var toAdds = this.Table.Columns.Where(k => !k.IsSystemDefault).ToList();
                    for (int i = 0; i < toAdds.Count; i += 2)
                    {
                        %>
                <tr>
                    <td class="td_lbl"><%=toAdds[i].DisplayName %>:</td>
                    <td class="td_cnt"><%=this.GetInput("_Add_", toAdds[i]) %></td>
                <%      if (i + 1 < toAdds.Count)
                        { %>
                    <td class="td_lbl"><%=toAdds[i+1].DisplayName %>:</td>
                    <td class="td_cnt"><%=this.GetInput("_Add_", toAdds[i+1]) %></td>
                <%      }
                        else{ %>
                    <td class="td_cnt" colspan="2">&nbsp;</td>
                <%      } %>
                </tr>
                <%   } %>
                <tr>
                    <td colspan="4" style="text-align:center; padding-top:10px">
                        <a href="javascript:void(0)" 
                            class="easyui-linkbutton do" 
                            id="btnAdd" 
                            iconcls="icon-ok" >确定</a>
                        <a href="javascript:void(0)" 
                            class="easyui-linkbutton close_dlg"
                            dlg="btnAdd"
                            iconcls="icon-cancel">关闭</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>

    <div id="dlgUpdate"
         class="easyui-dialog"
         style="width:800px;height:600px;padding:0px;"
         data-options="closed:true,resizable:true,modal:true,buttons:'#dlg-buttons',align:'center'">
        
        <form action="/dyajax/<%=this.Table.Name %>/update" method="post">
            <table id="tblUpdate" class="tb_grid" >
                <tr style="height:20px;"><td colspan="4">&nbsp;</td></tr>
                <%  var toUpdates = this.Table.Columns.Where(k => !k.IsSystemDefault && k.Edit).ToList();
                    for (int i = 0; i < toUpdates.Count; i += 2)
                    {
                        %>
                <tr>
                    <td class="td_lbl"><%=toUpdates[i].DisplayName %>:</td>
                    <td class="td_cnt"><%=this.GetInput("_Update_", toUpdates[i]) %></td>
                <%      if (i + 1 < toUpdates.Count)
                        { %>
                    <td class="td_lbl"><%=toUpdates[i+1].DisplayName %>:</td>
                    <td class="td_cnt"><%=this.GetInput("_Update_", toUpdates[i+1]) %></td>
                <%      }
                        else{ %>
                    <td class="td_cnt" colspan="2">&nbsp;</td>
                <%      } %>
                </tr>
                <%   } %>
                <tr>
                    <td colspan="4" style="text-align:center; padding-top:10px">
                        <a href="javascript:void(0)" 
                            class="easyui-linkbutton do" 
                            id="btnUpdate" 
                            iconcls="icon-ok" >确定</a>
                        <a href="javascript:void(0)" 
                            class="easyui-linkbutton close_dlg"
                            dlg="dlgUpdate"
                            iconcls="icon-cancel">关闭</a>
                    </td>
                </tr>
            </table>
            <input type="hidden" id="_Update_Id" name="Id" />
        </form>
    </div>

    <script type="text/ecmascript">
        var dlgAdd = new _dialog('#dlgAdd', '添加');
        dlgAdd.beforeDo = function () {
            var form = dlgAdd.dlg.find('form');
            if (!form.form('validate'))
                return false;

            var data = form.serialize();
            $.post('/dyajax/<%=this.Table.Name%>/add',
                   data,
                   function (res) {
                       if (res.state == 'ok') {
                           $.messager.alert('提示', '添加成功');
                           dlgAdd.close();
                           $('#table').datagrid('reload');
                       } else {
                           $.messager.alert('提示', '添加失败');
                       }
                   }, 'json');
        }

        var dlgUpdate = new _dialog('#dlgUpdate', '修改');
        dlgUpdate.beforeShow = function () {
            var selected = $('#table').datagrid('getSelected');
            if (selected) {
                $('#dlgUpdate input').val('');
                $('#dlgUpdate select').val('');
                $('#dlgUpdate textarea').val('');

                $('#_Update_Id').val(selected.Id);

                <% foreach (var col in toUpdates) {
                       switch (col.TypeId)
                       {
                           case GenericHtml.ColumnType.DECIMAL:
                               this.Response.Write(string.Format("$('#_Update_{0}').numberbox('setValue', selected.{0});\r\n", col.Name));
                               break;

                           case GenericHtml.ColumnType.STRING:
                               this.Response.Write(string.Format("$('#_Update_{0}').val(selected.{0});\r\n", col.Name));
                               break;
                       }
                   } %>
                
                return true;
            }
            else {
                $.messager.alert('提示', '请选择需要修改的行');
                return false;
            }
        }

        dlgUpdate.beforeDo = function () {
            var form = dlgUpdate.dlg.find('form');
            if (!form.form('validate'))
                return false;

            var data = form.serialize();
            $.post('/dyajax/<%=this.Table.Name%>/update',
                   data,
                   function (res) {
                       if (res.state == 'ok') {
                           $.messager.alert('提示', '修改成功');
                           dlgUpdate.close();
                           $('#table').datagrid('reload');
                       } else {
                           $.messager.alert('提示', '修改失败');
                       }
                   }, 'json');
        }

    </script>
    <script type="text/javascript">
        var toolbar = [{
            text: '添加',
            iconCls: 'icon-add',
            handler: function () {
                dlgAdd.show();
            }
        }, '-', {
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                dlgUpdate.show();
            }
        }, '-', {
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {
                var selected = $('#table').datagrid('getSelected');
                if (selected) {
                    $.messager.confirm('提示', '是否需要删除当前选择项?', function (r) {
                        if (r) {
                            $.post(
                                '/dyajax/<%=this.Table.Name%>/delete',
                                { Id: selected.Id },
                                function (res) {
                                    if (res.state == 'ok') {
                                        $.messager.alert('提示', '删除成功');
                                        $('#table').datagrid('reload');
                                    }
                                    else {
                                        $.messager.alert('提示', '删除失败');
                                    }
                                }, 'json');
                        }
                    });
                }
                else {
                    $.messager.alert('提示', '请选择需要删除的行');
                }
            }
        }, '-', {
            text: '启动工作流',
            handler: function () {
            }
        }, '-', {
            text: '查看工作流',
            handler: function () {
            }
        }];
    </script>
</body>
</html>